package com.gobang.controller;

import com.gobang.domain.dto.user.UpdateDisplayNameRequest;
import com.gobang.domain.vo.Result;
import com.gobang.domain.vo.auth.UserInfoVO;
import com.gobang.domain.vo.user.UserProfileVO;
import com.gobang.service.UserProfileService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/user/profile")
@RequiredArgsConstructor
public class UserProfileController {

    private final UserProfileService userProfileService;

    @GetMapping
    @PreAuthorize("hasAnyRole('USER','ADMIN')")
    public Result<UserProfileVO> getCurrentUserProfile() {
        UserProfileVO profile = userProfileService.getCurrentUserProfile();
        return Result.success(profile);
    }

    @PutMapping("/display-name")
    @PreAuthorize("hasAnyRole('USER','ADMIN')")
    public Result<Void> updateDisplayName(@Valid @RequestBody UpdateDisplayNameRequest request) {
        userProfileService.updateDisplayName(request.getDisplayName());
        return Result.success("昵称修改成功");
    }

    /**
     * 获取指定用户信息（需要认证）
     *
     * @param userId 用户ID
     * @return 用户信息
     */
    @GetMapping("/{userId}")
    public Result<UserInfoVO> getUserInfo(@PathVariable Long userId) {
        log.info("查询用户信息: userId={}", userId);
        UserInfoVO userInfo = userProfileService.getUserInfo(userId);
        return Result.success(userInfo);
    }

    /**
     * 获取当前登录用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/current")
    public Result<UserInfoVO> getCurrentUserInfo() {
        UserInfoVO userInfo = userProfileService.getCurrentUserInfo();
        return Result.success(userInfo);
    }
}
